updated and mostly functional