Complete rewrite; restored basic functionality.