some refactoring, code is now a bit better organized