refactoring + some labyrinth code