code refactor and cleanup