Full functionality achieved, minor error checking still needed. Some marked as TODOs in the comments.