prototyped and minor changes - thanks takmo