major refactor. needs heavy testing. fixes #31. fixes #31. fixes #32. fixes #33. fixes #34. fixes #35. fixes #36. fixes #37. fixes #38. fixes #39. fixes #40. fixes #41.