Debuggin and refactor