Fixing #error and ?error handling in popup

Also, added some unit tests for popup logic.