fixed memory error's from merge and upgraded ricardo's code a bit.