Major rewrite of the code to use the AMD pattern