Add contract asserts and improve readability