WIP
this was cherry picked from another branch, so it doesn't contain all the logic.
please do not touch:)
