refact continued (nearly finished)