Refactored the whole thing. Have no idea if it became more readable. Close #52.