initial version - working but ugly code