Cleaned up, still need to fix error checking.