first half - working version