Modified for easer read. Moved code to Paladin function file instead