Refactoring code in RPS branch

@gabebw @jsteiner