Refactor raining algorithm to central function