improved Javascript code and fixed minor bugs