Refactor code
More code refactor
Fix a few bugs