Refactor a bit
Add debug stuff