refactored a bit stored procedures