Adding unittests

Also made some small changes to emulator, fixed some small bugs