changed code in a more useful form based on php