Merged existing code - should be fully fleshed out now