cleaned structure and extracted common code. initial functionality working