Semi-final version of code; still need to do some documentation.