Added error logging and asserts