First attempt at refactoring code

See #71.