rewrote logic so it handles gridding better, tested, documented