major revision.. trimmed most of the code