Fix JSHint errors and minor optimizations