Overhaul: manual token parsing, split up functions