refactored prototype; working version