Cleaned up code. Minor perf optimization done.