Restructured. Added pattern matching function stuff