Abstract some calculations; work in all modern browsers