finished rewrite Everything should work properly now