Bug fixes and sync up with emulator code base