Just a bit of refactoring and some bug fixes.