refactoring wps's algorithm