Attempt by Patrik Bóna (@patrikbona)

I am afraid that I am not able to do more refactoring, so here is my pull request.