Added optional variable logger to function, needed for correctly handling multithreading