code cleanup + tests and fixed for old browsers