finalize changes and fix corner cases