Move big chunk of code upstream :)