Improves logging and removes code duplicity fixes #331