Lots of logic implementation fixes