Pulled out most important functions and got them working.