Refactor

The code is much cleaner... phew!