almost total rewrite, now works with multiple views and usage has been greatly simplified