Moved and adapted more stuff from tp