refactored, cleaned up