refactor and code cleanup