refactored code up to bb54. Almost there.