refactor and cleanup unsightly code