Numerous bug fixes; add more error handling code; get rid of const so that it works on Chrome