more working code + outline for other functions. still far from done.