Code refactoring (Fixes #17)

Still not clean, but cleaner.