Lots of updates, debugging and refactoring