Refactor a bit and fix reselection code.