Fix #11 and prepare for some code refactoring