Finished copying code from C to JavaScript