Remove unused code, refactor and cleanup