Overhaul from working version