Added better handling of arguments, and some cleanup