implement Top and Exit commands, some refactoring