Refractored based on Railsbp