Added better error checking. Fixed javascript interaction