Script instead of function

Created a script as I should have from the start. Also, some loops were
avoided by using matrices and element-wise operations.