WIP: integrate work from nevyn, still a little buggy