Edited funcs to avoid opening and closing files repeatedly in for loops

2 jobs for feature/move-function-to-losslib in 1 minute and 19 seconds (queued for 1 second)