Updated with some best practices

Fixed some potential problems following JavaScript best practices.