Added a bunch of code. Let me know if there are issues.