cleaned up, refactored a bit